package cn.iocoder.yudao.module.shop.service.staff;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import cn.iocoder.yudao.module.shop.controller.admin.staff.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.staff.StaffDO;
import cn.iocoder.yudao.module.shop.dal.dataobject.staff.StaffIdentityDO;
import cn.iocoder.yudao.module.shop.dal.dataobject.staff.StaffSalaryDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.shop.dal.mysql.staff.StaffMapper;
import cn.iocoder.yudao.module.shop.dal.mysql.staff.StaffIdentityMapper;
import cn.iocoder.yudao.module.shop.dal.mysql.staff.StaffSalaryMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.shop.enums.ErrorCodeConstants.*;

/**
 * 店铺员工 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class StaffServiceImpl implements StaffService {

    @Resource
    private StaffMapper staffMapper;
    @Resource
    private StaffIdentityMapper staffIdentityMapper;
    @Resource
    private StaffSalaryMapper staffSalaryMapper;

    @Override
    public Long createStaff(StaffSaveReqVO createReqVO) {
        // 插入
        StaffDO staff = BeanUtils.toBean(createReqVO, StaffDO.class);
        staffMapper.insert(staff);
        // 返回
        return staff.getId();
    }

    @Override
    public void updateStaff(StaffSaveReqVO updateReqVO) {
        // 校验存在
        validateStaffExists(updateReqVO.getId());
        // 更新
        StaffDO updateObj = BeanUtils.toBean(updateReqVO, StaffDO.class);
        staffMapper.updateById(updateObj);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteStaff(Long id) {
        // 校验存在
        validateStaffExists(id);
        // 删除
        staffMapper.deleteById(id);

        // 删除子表
        deleteStaffIdentityByStaffId(id);
        deleteStaffSalaryByStaffId(id);
    }

    private void validateStaffExists(Long id) {
        if (staffMapper.selectById(id) == null) {
            throw exception(STAFF_NOT_EXISTS);
        }
    }

    @Override
    public StaffDO getStaff(Long id) {
        return staffMapper.selectById(id);
    }

    @Override
    public PageResult<StaffDO> getStaffPage(StaffPageReqVO pageReqVO) {
        return staffMapper.selectPage(pageReqVO);
    }

    // ==================== 子表（店铺员工身份） ====================

    @Override
    public PageResult<StaffIdentityDO> getStaffIdentityPage(PageParam pageReqVO, Long staffId) {
        return staffIdentityMapper.selectPage(pageReqVO, staffId);
    }

    @Override
    public Long createStaffIdentity(StaffIdentityDO staffIdentity) {
        // 校验是否已经存在
        if (staffIdentityMapper.selectByStaffId(staffIdentity.getStaffId()) != null) {
            throw exception(STAFF_IDENTITY_EXISTS);
        }
        // 插入
        staffIdentityMapper.insert(staffIdentity);
        return staffIdentity.getId();
    }

    @Override
    public void updateStaffIdentity(StaffIdentityDO staffIdentity) {
        // 校验存在
        validateStaffIdentityExists(staffIdentity.getId());
        // 更新
        staffIdentity.setUpdater(null).setUpdateTime(null); // 解决更新情况下：updateTime 不更新
        staffIdentityMapper.updateById(staffIdentity);
    }

    @Override
    public void deleteStaffIdentity(Long id) {
        // 校验存在
        validateStaffIdentityExists(id);
        // 删除
        staffIdentityMapper.deleteById(id);
    }

    @Override
    public StaffIdentityDO getStaffIdentity(Long id) {
        return staffIdentityMapper.selectById(id);
    }

    private void validateStaffIdentityExists(Long id) {
        if (staffIdentityMapper.selectById(id) == null) {
            throw exception(STAFF_IDENTITY_NOT_EXISTS);
        }
    }

    private void deleteStaffIdentityByStaffId(Long staffId) {
        staffIdentityMapper.deleteByStaffId(staffId);
    }

    // ==================== 子表（店铺员工工资） ====================

    @Override
    public PageResult<StaffSalaryDO> getStaffSalaryPage(PageParam pageReqVO, Long staffId) {
        return staffSalaryMapper.selectPage(pageReqVO, staffId);
    }

    @Override
    public Long createStaffSalary(StaffSalaryDO staffSalary) {
        staffSalaryMapper.insert(staffSalary);
        return staffSalary.getId();
    }

    @Override
    public void updateStaffSalary(StaffSalaryDO staffSalary) {
        // 校验存在
        validateStaffSalaryExists(staffSalary.getId());
        // 更新
        staffSalary.setUpdater(null).setUpdateTime(null); // 解决更新情况下：updateTime 不更新
        staffSalaryMapper.updateById(staffSalary);
    }

    @Override
    public void deleteStaffSalary(Long id) {
        // 校验存在
        validateStaffSalaryExists(id);
        // 删除
        staffSalaryMapper.deleteById(id);
    }

    @Override
    public StaffSalaryDO getStaffSalary(Long id) {
        return staffSalaryMapper.selectById(id);
    }

    private void validateStaffSalaryExists(Long id) {
        if (staffSalaryMapper.selectById(id) == null) {
            throw exception(STAFF_SALARY_NOT_EXISTS);
        }
    }

    private void deleteStaffSalaryByStaffId(Long staffId) {
        staffSalaryMapper.deleteByStaffId(staffId);
    }

}